<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '420px'
    },
    options:{
      type:Array,
      default: [{
        title:{
          text:'南丁格尔玫瑰图',
          subtext: '表头提示文本',
          //表头位置
          left: 'left'
        }
      }]
    },
    pieData:{
      type:Array,
      default:[
        { value: 320, name: 'Industries' },
        { value: 240, name: 'Technology' },
        { value: 149, name: 'Forex' },
        { value: 100, name: 'Gold' },
        { value: 59, name: 'Forecasts' }
      ]
    },
  },
  data() {
    return {
      chart: null,
      newOption:null,
      newData:null,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons',{ devicePixelRatio: window.devicePixelRatio * 2 })
      let options = {
        title: {
          text: this.newOption.title.text,
          subtext: this.newOption.title.subtext,
          //表头位置
          left: this.newOption.title.left
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          show:true,
          //样式水平或竖直
          orient: 'vertical',
          x:'right',
          y:'center',
          itemHeight: 6,
          //间距
          itemGap: 18,
          formatter: function(name){
            const data = options.series[0].data;
            let total = 0;
            let tarValue;
            for (let i = 0; i < data.length; i++) {
              total += data[i].value;
              if (data[i].name === name) {
                tarValue = data[i].value;
              }
            }
            let v = tarValue;
            let p = Math.round(((tarValue / total) * 100));
            return `${name}  ${v}项`;
            // return `${name}  ${v}项(${p}  %)`;
          }
          ,
        },
        toolbox: {
          show: true,
          feature: {
            mark: { show: false },
            // 点击小图标数据详情的显示与隐藏
            dataView: { show: false, readOnly: false },
            // 刷新图表按键的显示与隐藏
            restore: { show: false },
            // 保存图表按键的显示与隐藏
            saveAsImage: { show: true }
          }
        },
        calculable : true,
        series: [
          {
            name: 'WEEKLY WRITE ARTICLES',
            type: 'pie',
            roseType: 'radius',
            radius: [30, 140],
            center: ['36%', '60%'],
            data: this.newData ,
            animationEasing: 'cubicInOut',
            animationDuration: 2200
          }
        ]
      };
      this.chart.setOption(options);
    }
  },
  watch:{
    options:{
      handler(newVal, oldVal) {
        if (newVal){
          this.newOption = this.options[0];
        }
      },immediate:true,deep:true
    },
    pieData:{
      handler(newVal, oldVal) {
        if (newVal){
          this.newData = this.pieData;
        }
      },immediate:true,deep:true
    },
  },
}
</script>
